#! /usr/bin/python

# Llibreries
import cgi, os, re, sys, string, time, shlex


def print_error(reason):
	print "<TITLE>Hay que Engrasarlo!!</TITLE>\n"
	print "<H1>Error: You did not provide a %s\n"%reason
	sys.exit()

# Programa principal
print "Content-type: text/html\n\n"
form = cgi.FieldStorage()
print "<title>Llistat de Lloguers</title>"

required_fields = ("userid", "password")

for k in required_fields:
	if not form.has_key(k):
		print_error(k)



userok = "username" == form.getvalue("userid", "")
passok = "password" == form.getvalue("password", "")

if userok and passok:
	print "<h1>Benvingut ", form.getvalue("userid", ""), "</h1>"
	data = open("transaccions.txt","r")
	num = 1
	print "<ol>"
	for line in data:
		aux = shlex.split(line)
		t = " -->Model : "+aux[0]+" Motor: " +aux[1] + " Dies: " +aux[2]+ " Vehicles: " +aux[3]+ " Descompte: " + aux[4] +" Total "+  aux[5]+ " Euros"
		print "<li>Transaccio numero "
		print num 
		num = num+1
		print t 
		print "</li>"
	print "</ol>"
else:
	print "<h1>Usuari o password invalids</h1>"

print "<a href=\"/main.html\">Tornar a la plana principal</a>"

